import { ref } from'vue';
import { defineStore } from "pinia";

export const useSearchHistory = defineStore('address',()=>{
  const historyData = ref<string[]>([])

  const setSearchHistory = (val: string) => {
    const newSearch = historyData.value.filter(item => item !== val)
    newSearch.unshift(val);
    // 只要12个
    historyData.value = newSearch.slice(0,12)
  }

  const cleanSearchHistory = () => {
    historyData.value = []
  }

  return {
    historyData,
    setSearchHistory,
    cleanSearchHistory,
  }
},
{
  // 小程序端配置
  persist:{
    storage:{
      getItem(key) {
        return uni.getStorageSync(key)
      },
      setItem(key, value) {
        uni.setStorageSync(key, value)
      }
    }
  }
})